package com.ysj.demo.accompanynetwork.mvp.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.ysj.demo.accompanynetwork.activity.LoginActivity;
import com.ysj.demo.accompanynetwork.bean.LoginBean;
import com.ysj.demo.accompanynetwork.bean.LoginResultBean;
import com.ysj.demo.accompanynetwork.bean.RestRsp;
import com.ysj.demo.accompanynetwork.mvp.common.OnCommonListener;
import com.ysj.demo.accompanynetwork.mvp.model.LoginModel;
import com.ysj.demo.accompanynetwork.mvp.view.LoginView;

/**
 * Created by 80530 on 2018/8/18.
 */

public class LoginPresenter {

    private LoginModel mLoginModel;

    private LoginView mLoginView;

    public LoginPresenter() {
        mLoginModel = new LoginModel();
    }

    public void bind(LoginActivity loginView) {
        mLoginView = loginView;
    }

    /**
     * 登录业务
     */
    public void login() {
        String account = mLoginView.getAccount();
        String password = mLoginView.getPassword();
        Log.e("TAG", "account:" + account + ",password" + password);
        if (checkParameter(account, password)) {
            doSomePrepare();
            //登录 ，需要处理数据，所有要在model中执行
            LoginBean bean = new LoginBean(account, password);
            mLoginModel.LoginTask(bean, new OnCommonListener() {
                @Override
                public void onSuccessListener(Object data) {
                    RestRsp<LoginResultBean> info = (RestRsp<LoginResultBean>) data;
                    mLoginView.loginSuccess(info);
                }

                @Override
                public void onFailListener(Throwable t) {

                }
            });
        }
    }

    /**
     * 做一些准备
     */
    private void doSomePrepare() {
        //这里可以设置按钮不可点击！否则一直点击登录也是不好
    }

    /**
     * 检测参数是否是否为空~~~
     *
     * @param account
     * @param password
     * @return
     */
    private boolean checkParameter(String account, String password) {
        if (TextUtils.isEmpty(account) | TextUtils.isEmpty(password)) {
            mLoginView.showVerifyFailed();//提示错误
            return false;
        } else if (!checkNetwork()) {
            mLoginView.showNetworkError();//提示网络错误
            return false;
        }
        return true;
    }

    /**
     * 检测网络是否可用
     */
    public boolean checkNetwork() {
        return true;//先显示可以联网，实际中要用代码判断
    }
}
